// request.js
const BASE_URL = 'https://shixizhixue.com/api';
export const img_url = 'https://shixizhixue.com';
// const BASE_URL = 'https://shuati.hhphp.top/api';
// export const img_url = 'https://shuati.hhphp.top';
// 封装的请求函数
export function request(url, method = 'GET', data = {}, header = {}) {
	wx.showLoading()
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			method: method,
			data: data,
			header: {
				'Content-Type': 'application/json',
				'token': uni.getStorageSync('token')
			},
			success: (res) => {
				wx.hideLoading()
				if (res.statusCode >= 200 && res.statusCode < 300) {
					// 请求成功
					resolve(res.data);
				} else {
					// 请求失败，但服务器返回了错误信息
					reject(res.data);
				}
				if(res.data.code==401){
					// uni.showToast({
					// 	title:res.data.msg,
					// 	icon:"none"
					// })
					// setTimeout(function(){
					// 	uni.redirectTo({
					// 		url:'/subpages/login/login'
					// 	})
					// },2000)
				}
			},
			fail: (err) => {
				// 请求失败，可能是网络错误等
				reject(err);
			}
		});
	});
}

// 封装 GET 请求
export function get(url, data = {}, header = {}) {
	return request(url, 'GET', data, header);
}

// 封装 POST 请求
export function post(url, data = {}, header = {}) {
	return request(url, 'POST', data, header);
}

// 封装 PUT 请求
export function put(url, data = {}, header = {}) {
	return request(url, 'PUT', data, header);
}

// 封装 DELETE 请求
export function del(url, data = {}, header = {}) {
	return request(url, 'DELETE', data, header);
}
